var CryptoJS = require('crypto-js');



//设置cookie
export function setCookie(username, psw, exdays) {
    // Encrypt，加密账号密码
    var cipherUsername = CryptoJS.AES.encrypt(
        username + '',
        "secretkey666"
    ).toString();
    var cipherPsw = CryptoJS.AES.encrypt(psw + '', "secretkey666").toString();
    // console.log(cipherUsername + '/' + cipherPsw)//打印一下看看有没有加密成功

    var exdate = new Date(); //获取时间
    exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
    //字符串拼接cookie，为什么这里用了==，因为加密后的字符串也有个=号，影响下面getcookie的字符串切割，你也可以使用更炫酷的符号。
    window.document.cookie =
        "currentusername" +
        "==" +
        cipherUsername +
        ";path=/;expires=" +
        exdate.toGMTString();
    window.document.cookie =
        "password" +
        "==" +
        cipherPsw +
        ";path=/;expires=" +
        exdate.toGMTString();
}

//读取cookie
export function getCookie(self) {
    if (document.cookie.length > 0) {
        var arr = document.cookie.split("; "); //这里显示的格式请根据自己的代码更改
        for (var i = 0; i < arr.length; i++) {
            var arr2 = arr[i].split("=="); //根据==切割
            //判断查找相对应的值
            if (arr2[0] == "currentusername") {
                // Decrypt，将解密后的内容赋值给账号
                var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey666");
                self.loginForm.username = bytes.toString(CryptoJS.enc.Utf8);
            } else if (arr2[0] == "password") {
                // Decrypt，将解密后的内容赋值给密码
                var bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey666");
                self.loginForm.password = bytes.toString(CryptoJS.enc.Utf8);
            }
        }
    }
}
//清除cookie
export function clearCookie() {
    setCookie("", "", -1);
}

